package com.example.app5.bitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.app5.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 网络图片下载
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ExecutorService threadPool;

    private Button xz,xz2,xz3,xz4,xz5;
    private ImageView tp,tp2,tp3,tp4,tp5;
    private Handler handler;

    private Bitmap bitmap1,bitmap2,bitmap3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建线程池
        threadPool = Executors.newFixedThreadPool(5);

        xz = findViewById(R.id.xz);
        tp = findViewById(R.id.tp);

        xz2 = findViewById(R.id.xz2);
        tp2 = findViewById(R.id.tp2);


        xz3 = findViewById(R.id.xz3);
        tp3 = findViewById(R.id.tp3);


        xz4 = findViewById(R.id.xz4);
        tp4 = findViewById(R.id.tp4);


        xz5 = findViewById(R.id.xz5);
        tp5 = findViewById(R.id.tp5);



        //图片1
        xz.setOnClickListener(this);
        xz2.setOnClickListener(this);
        xz3.setOnClickListener(this);
        xz4.setOnClickListener(this);
        xz5.setOnClickListener(this);


        handler = new Handler(getMainLooper()) {



            //接收消息
            @Override
            public void handleMessage(Message msg) {
                //判断消息的形式
                if (msg.what == 1) {
                    //获取消息的对象
                    bitmap1 = (Bitmap) msg.obj;
                    //UI展示
                    tp.setImageBitmap(bitmap1);
                } else if (msg.what == 2) {
                    //获取消息的对象
                     bitmap2 = (Bitmap) msg.obj;
                    //UI展示
                    tp2.setImageBitmap(bitmap2);
                }else if (msg.what == 3) {
                    //获取消息的对象
                    bitmap3 = (Bitmap) msg.obj;
                    //UI展示
                    tp3.setImageBitmap(bitmap3);
                }else if (msg.what == 4) {
                    //获取消息的对象
                    Bitmap bitmap = (Bitmap) msg.obj;
                    //UI展示
                    tp4.setImageBitmap(bitmap);
                }else if (msg.what == 5) {
                    //获取消息的对象
                    Bitmap bitmap = (Bitmap) msg.obj;
                    //UI展示
                    tp5.setImageBitmap(bitmap);
                }
            }
        };


    }


    /**
     * https://s1.aigei.com/src/img/png/86/86c0f516a06441ee97fd2f0808cfead9.png?imageMogr2/auto-orient/thumbnail/!282x158r/gravity/Center/crop/282x158/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:m39WQNq5rJbngZ1mLkJrvBRDajk=
     *
     * https://s1.aigei.com/src/img/jpg/1e/1e3ea5677800422eb6a419ff918f81eb.jpg?imageMogr2/auto-orient/thumbnail/!282x320r/gravity/Center/crop/282x320/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:IzmiYcvU2puEYHXo6DwZlt_oZMM=
     *
     * https://s1.aigei.com/src/img/jpeg/ad/ad8a3f1b93f8405f82028806f7250b9f.jpeg?imageMogr2/auto-orient/thumbnail/!167x282r/gravity/Center/crop/167x282/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:4ivrlAkBeffkxJcSICpzVoiY3ic=
     *
     * https://s1.aigei.com/src/img/jpg/12/12879e1d1c654d4b98d0047332f29be0.jpg?imageMogr2/auto-orient/thumbnail/!240x320r/gravity/Center/crop/240x320/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:pGHS0er7OhtLYc0rw8gUrwn3tN8=
     *
     * https://s1.aigei.com/src/img/jpg/ee/ee090d7aa26d4271bf232e400be08d6c.jpg?imageMogr2/auto-orient/thumbnail/!282x320r/crop/!282x320a0a0/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:efr7yaMiVjmLZAOFbfZ8VkMH6wM=
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.xz) {
            if (bitmap1 == null) {
                String imageUrl = "https://s1.aigei.com/src/img/png/86/86c0f516a06441ee97fd2f0808cfead9.png?imageMogr2/auto-orient/thumbnail/!282x158r/gravity/Center/crop/282x158/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:m39WQNq5rJbngZ1mLkJrvBRDajk=";
                DownloadTask downloadTask = new DownloadTask(imageUrl, 1);
                //启动线程池
                threadPool.execute(downloadTask);
            } else {
                showToast("该图片1以下载");
            }


        } else if (v.getId() == R.id.xz2) {
            if (bitmap2 == null) {
                String imageUrl = "https://s1.aigei.com/src/img/png/bb/bb63410fa09d4d29b5c10780df91b81c.png?imageMogr2/auto-orient/thumbnail/!282x320r/crop/!282x320a0a0/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:6PJty0rtqF4kFa0-YNCc8NrxNWo=";
                DownloadTask downloadTask = new DownloadTask(imageUrl, 2);
                //启动线程池
                threadPool.execute(downloadTask);
            } else {
                showToast("该图片2以下载");
            }

        }else if (v.getId() == R.id.xz3) {
            if (bitmap3 == null) {
                String imageUrl = "https://s1.aigei.com/src/img/jpeg/ad/ad8a3f1b93f8405f82028806f7250b9f.jpeg?imageMogr2/auto-orient/thumbnail/!167x282r/gravity/Center/crop/167x282/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:4ivrlAkBeffkxJcSICpzVoiY3ic=";
                DownloadTask downloadTask = new DownloadTask(imageUrl, 3);
                //启动线程池
                threadPool.execute(downloadTask);
            } else {
                showToast("该图片3以下载");
            }


        }else if (v.getId() == R.id.xz4) {
            String imageUrl = "https://s1.aigei.com/src/img/jpg/12/12879e1d1c654d4b98d0047332f29be0.jpg?imageMogr2/auto-orient/thumbnail/!240x320r/gravity/Center/crop/240x320/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:pGHS0er7OhtLYc0rw8gUrwn3tN8=";
            DownloadTask downloadTask = new DownloadTask(imageUrl,4);
            //启动线程池
            threadPool.execute(downloadTask);
        }else if (v.getId() == R.id.xz5) {
            String imageUrl = "https://s1.aigei.com/src/img/jpg/ee/ee090d7aa26d4271bf232e400be08d6c.jpg?imageMogr2/auto-orient/thumbnail/!282x320r/crop/!282x320a0a0/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:efr7yaMiVjmLZAOFbfZ8VkMH6wM=";
            DownloadTask downloadTask = new DownloadTask(imageUrl,5);
            //启动线程池
            threadPool.execute(downloadTask);
        }

    }

    class DownloadTask implements Runnable {
        private String imageUrl;

        private int count;

        public DownloadTask(String imageUrl,int count) {
            this.imageUrl = imageUrl;
            this.count=count;
        }

        @Override
        public void run() {
            try {
                //下载图片
                InputStream inputStream = new java.net.URL(imageUrl).openStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                //创建消息
                Message message = Message.obtain();
                message.what = count;
                message.obj = bitmap;
                //把下载的图片以消息的形式发送
                handler.sendMessage(message);

                //关闭流
                inputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        threadPool.shutdown(); // 关闭线程池
    }

    public void showToast(String msg) {
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
}
